﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TetrisLibrary.extras;

namespace Tetris {

    public partial class FrmMain : Form {

        private Point mousePoint = new Point();
        private bool isPressOnWindow = false;

        private int operatorButtonState = 0; //0 - 未使用状态， 1 - 继续游戏， -1 - 暂停游戏

        public FrmMain() {
            InitializeComponent();
            tetrisView.OnPreviewNextBlockEvent += shape => {
                tetrisBlockPreviewView.TetrisShape = shape;
            };
            tetrisView.OnGameOverEvent += () => {
                operatorButtonState = 0;
                btnGameOperator.Text = "重新开始游戏";
            };
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
            switch (keyData) {
                case Keys.A: //左
                case Keys.Left:
                    tetrisView.MoveTo(Direction.Left);
                    break;
                case Keys.W: //上
                case Keys.Up:
                    tetrisView.MakeShapeTurn();
                    break;
                case Keys.D: //右
                case Keys.Right:
                    tetrisView.MoveTo(Direction.Right);
                    break;
                case Keys.S: //下
                case Keys.Down:
                    tetrisView.MoveTo(Direction.Down);
                    break;
                default:
                    return base.ProcessCmdKey(ref msg, keyData);
            }
            return true;
        }

        private void btnGameOperator_Click(object sender, EventArgs e) {
            if (operatorButtonState == 0) {
                operatorButtonState = 1;
                btnGameOperator.Text = "暂停游戏";
                if(tetrisView.IsGameFinished)
                    tetrisView.ClearTetrisMap();
                tetrisView.StartGame();
            }else if (operatorButtonState == 1) {
                operatorButtonState = -1;
                btnGameOperator.Text = "继续游戏";
                tetrisView.GoPlayGame();
            }else if (operatorButtonState == -1) {
                operatorButtonState = 1;
                btnGameOperator.Text = "暂停游戏";
                tetrisView.PauseGame();
            }
        }

        private void FrmMain_MouseDown(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Left) {
                mousePoint.X = -e.X;
                mousePoint.Y = -e.Y;
                isPressOnWindow = true;
            }
        }

        private void FrmMain_MouseMove(object sender, MouseEventArgs e) {
            if (isPressOnWindow) {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mousePoint.X, mousePoint.Y); //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void FrmMain_MouseUp(object sender, MouseEventArgs e) {
            if (isPressOnWindow) {
                isPressOnWindow = false;
            }
        }

        private void FrmMain_SizeChanged(object sender, EventArgs e)
        {
            this.Region = new Region(GetRoundedRectPath(new Rectangle(0, 0, Width, Height), 10));
        }

        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            //   左上角      
            path.AddArc(arcRect, 180, 90);
            //   右上角      
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            //   右下角      
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            //   左下角      
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }

        private void FrmMain_Paint(object sender, PaintEventArgs e) {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.DrawPath(new Pen(Color.SlateGray, 2f), GetRoundedRectPath(new Rectangle(0, 0, Width - 1, Height - 1), 10));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
    }
}
